package com.jn.algorithm.day006.Question20;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author 江南大学1033190417
 * @date 2022/4/20 15:57
 */
public class Solution {

    Map<Character, Character> pairs = new HashMap<Character, Character>() {{
        put(')', '(');
        put(']', '[');
        put('}', '{');
    }};

    public boolean isValid(String s) {
        int length = s.length();
        if (length%2==1){
            return false;
        }
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (pairs.containsKey(c)){
                if (stack.isEmpty()||stack.peek()!=pairs.get(c)){
                    return false;
                }
                stack.pop();
            }else {
                stack.add(c);
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Solution solution=new Solution();
        String s="{[](})";
        System.out.println(solution.isValid(s));
    }
}
